package com.chengcang.admin.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chengcang.admin.modules.sys.dao.SysOrderProductTypeSnapshotDao;
import com.chengcang.admin.modules.sys.dto.orderSnapshotDTO.SysOrderProductTypeSnapshotDTO;
import com.chengcang.admin.modules.sys.entity.orderSnapshotEntity.SysOrderProductTypeSnapshotEntity;
import com.chengcang.admin.modules.sys.service.SysOrderProductTypeSnapshotService;
import com.chengcang.common.service.impl.CrudServiceImpl;
import com.chengcang.common.utils.ConvertUtils;
import com.chengcang.common.utils.TreeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author Zhanli xy321231@163.com
 * @since 1.0.0 2023-01-14
 */
@Service
public class SysOrderProductTypeSnapshotServiceImpl extends CrudServiceImpl<SysOrderProductTypeSnapshotDao, SysOrderProductTypeSnapshotEntity, SysOrderProductTypeSnapshotDTO> implements SysOrderProductTypeSnapshotService {

    @Autowired
    private SysOrderProductTypeSnapshotDao sysOrderProductTypeSnapshotDao;

    @Override
    public QueryWrapper<SysOrderProductTypeSnapshotEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SysOrderProductTypeSnapshotEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    public List<Long> getSecondIdsById(Long id){
        try{
            return sysOrderProductTypeSnapshotDao.selectList( new LambdaQueryWrapper<SysOrderProductTypeSnapshotEntity>()
                    .eq(SysOrderProductTypeSnapshotEntity::getPid,id)).stream().map(SysOrderProductTypeSnapshotEntity::getId).collect(Collectors.toList());
        }catch (Exception e){
            log.error(e.getMessage(), e);
            throw new RuntimeException("查询二级分类异常!");
        }
    }


    @Override
    public List<SysOrderProductTypeSnapshotDTO> getTree() {
        List<SysOrderProductTypeSnapshotEntity> allType = sysOrderProductTypeSnapshotDao.selectList(new LambdaQueryWrapper<SysOrderProductTypeSnapshotEntity>());
        List<SysOrderProductTypeSnapshotDTO> sysProductTypeDTOS = ConvertUtils.sourceToTarget(allType, SysOrderProductTypeSnapshotDTO.class);
        return TreeUtils.build(sysProductTypeDTOS);
    }

}